Some css parsing tests
authorMatthias Clasen <mclasen@redhat.com>
Thu, 18 Nov 2010 07:00:53 +0000 (02:00 -0500)
committerCarlos Garnacho <carlosg@gnome.org>
Sat, 4 Dec 2010 14:39:16 +0000 (15:39 +0100)
gtk/tests/Makefile.am
gtk/tests/stylecontext.c [new file with mode: 0644]
gtk/tests/test.css [new file with mode: 0644]

index e97a30f9cb7415e77c41517414c3bf5be02d8511..8b326c62d4d5688f79ec10633f3aa04338c4322f 100644 (file)
@@ -100,6 +100,11 @@ SAMPLE_PROGS = gtk-example-application
 gtk_example_application_SOURCES        = gtk-example-application.c
 gtk_example_application_LDADD  = $(progs_ldadd)
 
+TEST_PROGS                     += stylecontext
+stylecontext_SOURCES            = stylecontext.c
+stylecontext_LDADD              = $(progs_ldadd)
+EXTRA_DIST += test.css
+
 
 EXTRA_DIST +=                          \
        file-chooser-test-dir/empty     \
diff --git a/gtk/tests/stylecontext.c b/gtk/tests/stylecontext.c
new file mode 100644 (file)
index 0000000..c2c20f4
--- /dev/null
@@ -0,0 +1,161 @@
+#include <gtk/gtk.h>
+
+static void
+test_parse_empty (void)
+{
+  GtkCssProvider *provider;
+  GError *error;
+  gboolean res;
+
+  provider = gtk_css_provider_new ();
+  error = NULL;
+  res = gtk_css_provider_load_from_data (provider, "", -1, &error);
+
+  g_assert (res);
+  g_assert_no_error (error);
+  g_clear_error (&error);
+
+  g_object_unref (provider);
+}
+
+static void
+test_parse_at (void)
+{
+  GtkCssProvider *provider;
+  GError *error;
+  gboolean res;
+  gint i;
+  const gchar *valid[] = {
+    "@import \"test.css\";",
+    "@import 'test.css';",
+    "@import url(\"test.css\");",
+    "@import url('test.css');",
+    "@import\nurl (\t\"test.css\" ) ;",
+    NULL
+  };
+
+  const gchar *invalid[] = {
+    "@import test.css ;",
+    "@import url ( \"test.css\" xyz );",
+    "@import url(\");",
+    "@import url(');",
+    "@import url(\"abc');",
+    "@ import ;",
+    NULL
+  };
+
+  error = NULL;
+  for (i = 0; valid[i]; i++)
+    {
+      provider = gtk_css_provider_new ();
+      res = gtk_css_provider_load_from_data (provider, valid[i], -1, &error);
+      g_assert_no_error (error);
+      g_assert (res);
+
+      g_object_unref (provider);
+   }
+
+  for (i = 0; invalid[i]; i++)
+    {
+      provider = gtk_css_provider_new ();
+      res = gtk_css_provider_load_from_data (provider, invalid[i], -1, &error);
+      g_assert_error (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_FAILED);
+      g_assert (!res);
+      g_object_unref (provider);
+      g_clear_error (&error);
+   }
+}
+
+static void
+test_parse_selectors (void)
+{
+  GtkCssProvider *provider;
+  GError *error;
+  gboolean res;
+  gint i;
+  const gchar *valid[] = {
+    "* {}",
+    "E {}",
+    "E F {}",
+    "E > F {}",
+    "E#id {}",
+    "#id {}",
+    "tab:first-child {}",
+    "tab:last-child {}",
+    "tab:nth-child(first) {}",
+    "tab:nth-child(last) {}",
+    "tab:nth-child(even) {}",
+    "tab:nth-child(odd) {}",
+    "tab:sorted {}",
+    ".some-class {}",
+    ".some-class.another-class {}",
+    ".some-class .another-class {}",
+    "E * {}",
+    "E .class {}",
+    "E > .foo {}",
+    "E > #id {}",
+    "E:active {}",
+    "E:prelight {}",
+    "E:hover {}",
+    "E:selected {}",
+    "E:insensitive {}",
+    "E:inconsistent {}",
+    "E:focused {}",
+    "E:active:prelight {}",
+    "* > .notebook tab:first-child .label:focused {}",
+    "E, F {}",
+    NULL
+  };
+
+  const gchar *invalid[] = {
+    /* nth-child and similar pseudo classes can only
+     * be used with regions, not with types
+     */
+    "E:first-child {}",
+    "E:last-child {}",
+    "E:nth-child(first) {}",
+    "E:nth-child(last) {}",
+    "E:nth-child(even) {}",
+    "E:nth-child(odd) {}",
+    "E:sorted {}",
+    /* widget state pseudo-classes can only be used for
+     * the last element
+     */
+    "E:focused tab {}",
+     NULL
+  };
+
+  error = NULL;
+  for (i = 0; valid[i]; i++)
+    {
+      provider = gtk_css_provider_new ();
+      res = gtk_css_provider_load_from_data (provider, valid[i], -1, &error);
+      g_assert_no_error (error);
+      g_assert (res);
+
+      g_object_unref (provider);
+   }
+
+  for (i = 0; invalid[i]; i++)
+    {
+      provider = gtk_css_provider_new ();
+      res = gtk_css_provider_load_from_data (provider, invalid[i], -1, &error);
+      g_assert_error (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_FAILED);
+      g_assert (!res);
+      g_object_unref (provider);
+      g_clear_error (&error);
+   }
+}
+
+int
+main (int argc, char *argv[])
+{
+  g_type_init ();
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/style/parse/empty", test_parse_empty);
+  g_test_add_func ("/style/parse/at", test_parse_at);
+  g_test_add_func ("/style/parse/selectors", test_parse_selectors);
+
+  return g_test_run ();
+}
diff --git a/gtk/tests/test.css b/gtk/tests/test.css
new file mode 100644 (file)
index 0000000..e69de29